package org.onion.pattern.kit.validation.annotation.validator;


import org.onion.pattern.kit.validation.annotation.IsPropName;
import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;
import org.apache.commons.lang3.StringUtils;

import java.util.regex.Pattern;


public class PropNameValidator implements ConstraintValidator<IsPropName, String> {
    private final static Pattern PATTERN = Pattern.compile("^[a-zA-Z][a-zA-Z0-9_]*$");

    @Override
    public void initialize(IsPropName isUserName) {

    }

    @Override
    public boolean isValid(String s, ConstraintValidatorContext constraintValidatorContext) {
        if (StringUtils.isBlank(s)) {
            return true;
        } else if (PATTERN.matcher(s).find()) {
            return true;
        }
        return false;
    }
}
